package com.school.gift.controller;

import com.github.pagehelper.PageInfo;
import com.school.gift.domain.*;
import com.school.gift.model.EvaluateModel;
import com.school.gift.model.GoodsModel;
import com.school.gift.service.*;
import com.school.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.common.support.base.Page;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;

import java.util.List;

/**
 * Created by 执笔 on 2017/3/5.
 */
@RequestMapping("goods")
@Controller
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsImgService goodsImgService;

    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private UserService userService;


    /**
     * 详情页
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.selectByPK(id);
        List<GoodsImg> goodsImgs = goodsImgService.selectByGoods(id);
        PageInfo<EvaluateModel> evaluatePageInfo = evaluateService.selectByGoods(id, new Page(1, 10));
        setModelAttribute(model, goods, goodsImgs, evaluatePageInfo);
        return "goods/detail";
    }

    @RequestMapping("list")
    public String list(Goods goods, Model model,Page page) {
        Example example = ExampleUtil.getInstance()
                .addOrder("g.sort", ExampleType.OrderType.DESC)
                .addParam("g.firstcategory",goods.getFirstcategory())
                .addParam("g.secondcategory",goods.getSecondcategory())
                .addParam("g.type",goods.getType())
                .addParam("g.status",STATE_1)
                .getExample();
        PageInfo<GoodsModel> pageInfo = goodsService.selectModel(example, page);
        setModelAttribute(model, pageInfo);
        return "goods/list";
    }

}
